// 所有 ts 原始类型类型注解的示例
// 原始类型-----------------------------------------
const str: string = 'hello';
const num: number = 123;
const bool: boolean = true;
const dataNull: null = null;
const dataUndefined: undefined = undefined;

// 数组类型---------------------------------------
// ts 由字符串或数字或布尔值组成的数组
// type MyArray = (string | number | boolean)[]
// const arr: (string | number | boolean)[] = [1, 2, '你好世界', 4, true, 6, 7, 8, 9, 10]
// const arr2: (string | number | boolean)[] = [1, 2, '你好世界', 4, true, 6, 7, 8, 9, 10]
// const arr3: (string | number | boolean)[] = [1, 2, '你好世界', 4, true, 6, 7, 8, 9, 10]
// const arr4: (string | number | boolean)[] = [1, 2, '你好世界', 4, true, 6, 7, 8, 9, 10]

// 可以简化成类型别名的写法(其中 | 是联合类型的意思)
type MyArray = (string | number | boolean)[]
const arr: MyArray = [1, 2, '你好世界', 4, true, 6, 7, 8, 9, 10]
const arr2: MyArray = [1, 2, '你好世界', 4, true, 6, 7, 8, 9, 10]
const arr3: MyArray = [1, 2, '你好世界', 4, true, 6, 7, 8, 9, 10]
const arr4: MyArray = [1, 2, '你好世界', 4, true, 6, 7, 8, 9, 10]


// 函数类型--------------------------------
// 分别制定参数类型和返回值类型
const add = (a: number, b: number): number => {
    return a + b
}

console.log(add(666, 5));

// 类型别名的方式, 同时制定参数和返回值
type AddFn = (a: number, b: number) => number

const myAddFn: AddFn = (a, b) => {
    return a + b
}

// void 返回值类型
const myFn = (a:number): void => {
    console.log(a); 
}

// 可选参数
// 如果一个函数, 有参数是可选的, 则需要使用 ? 表示可选参数
const myFn2 = (a: number, b?: number): number => {
    b = b || 0
    return a + b
}

type StudentType = {
    name: string;
    age: number;
    gender: string;
    // 对象里面的方法类型可以有两种写法
    // saySomething: (msg: string) => void;
    saySomething(msg: string): void;
    graduateYear?: number;
}


const studentA: StudentType = {
    name: '张三',
    age: 18,
    gender: '男',
    saySomething: (msg: string) => {
        console.log(msg)
    }
}

const studentB: StudentType = {
    name: '李四',
    age: 19,
    gender: '女',
    saySomething: (msg: string) => {
        console.log(msg)
    }
}


const studentC: StudentType = {
    name: '王五',
    age: 20,
    gender: '男',
    saySomething: (msg: string) => {
        console.log(msg)
    },
    graduateYear: 2021
}